<?php
declare(strict_types=1);

namespace App\ZxAdmin\Users;

use App\Controller;
use Field\SysCateAccessConst;
use Field\SysCateAccessRow;
use Field\SysCateConst;
use Field\SysRoleAuthConst;
use Field\SysRoleAuthRow;
use Framework\Libs\Model;

// hook controller_zxadmin_content_cate_access_use.php

/**
 * @title   分类管理
 * @auth    true
 * @login   true
 * @menu    false
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    1
 */
class RoleAuth extends Controller
{
    // hook controller_zxadmin_content_cate_access_public_start.php

    // hook controller_zxadmin_content_cate_access_public_end.php
    public const Conf = [
        //'Index' => 'CateID',
        //'TreeData' => true,
        //'rowField' => 'CateID',
        //'parentField' => 'ParentCateID',
        'pageSize' => 25,
        'pageSizes' => [10, 15, 20, 25, 30, 50, 100, 200, 500],
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 1,
        'hideDo' => 1,
        'add' => 0,
        'start' => 1,
        'stop' => 1,
        'delete' => 0,
        'import' => 0,
        'export' => 0
    ];
    /**
     * @title   列操作
     */
    public const Tools = [];
    /**
     * @title   列配置
     */
    public const Columns = [];
    /**
     * @title   筛选
     */
    public const Filter = [];
    /**
     * @title   添加内容
     */
    public const AddForm = [];
    /**
     * @title   编辑内容
     */
    public const EditForm = [];

    /**
     * @title  数据库连接
     * @rank    99
     */
    public function action_Conf_PUT()
    {
        // hook controller_zxadmin_content_cate_access_index_start.php
        $Columns = [
            [
                'label' => "ID",
                'prop' => "RoleID",
                'width' => "60"
            ],
            [
                'label' => "节点",
                'prop' => "Node",
                'width' => "200"
            ],
            [
                'label' => "名称",
                'prop' => "Title",
                'width' => "100"
            ],
            [
                'type' => "dict",
                'label' => "状态",
                'prop' => "Status",
                'editable'=>1,
                'width' => "60"
            ],
            ];


        // hook controller_zxadmin_content_cate_access_index_end.php

        $this->response('0000', ['Tabs' => [], 'Conf' => self::Conf, 'Filter' => self::Filter, 'Tools' => self::Tools, 'Columns' => $Columns]);

    }

    /**
     * @title  数据表
     * @rank    99
     */
    public function action_Index()
    {
        // hook controller_zxadmin_content_cate_access_index_start.php
        $RoleID = $this->request->param('RoleID', 0);
        $this->CheckEmpty([$RoleID],['权限组ID']);
        $RoleID==1 && $this->response('0003',[],'超管拥有所有权限，无需设置');
        $roles =  $this->SysMenu->select();
        $auth = $this->SysRoleAuth->select([SysRoleAuthConst::RoleID => $RoleID],[],0,0,'*','Node');
        $add=[];
        foreach ($roles as $role){
            $Node = (str_ends_with($role['Node'],'/index/index')  ? str_replace('/index/index','/index',$role['Node']) :$role['Node']).($role['Method']?'_'.$role['Method']:'');
            if(!isset($auth[$Node])){
                $auth[$Node] =(new SysRoleAuthRow(['RoleID' => $RoleID, 'Node' => $Node,'Status'=>0]))->toArray();
                $add[]=$auth[$Node];
            }
            $auth[$Node]['Title']=$role['Name'];
            $auth[$Node]['_index']=$Node;
        }
        !empty($add) && $this->SysRoleAuth->insertALL($add);

        // hook controller_zxadmin_content_cate_access_index_end.php
        $this->response('0000', ['data'=>array_values($auth)]);
    }



    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {
        // hook controller_zx_admin_users_role_enable_start.php
        $data = $this->request->param('data', []);
        $cond = [];
        foreach ($data as $row){
            $cond['OR'][] = [SysRoleAuthConst::RoleID=>$row['RoleID'],SysRoleAuthConst::Node=>$row['Node']];
        }
        $this->CheckEmpty([$cond], ['操作数据为空']);

        $r = $this->SysRoleAuth->update($cond, [SysRoleAuthConst::Status => 1]);
        // hook controller_zx_admin_users_role_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller_zx_admin_users_role_disable_start.php
        $data = $this->request->param('data', []);
        $cond = [];
        foreach ($data as $row){
            $cond['OR'][] = [SysRoleAuthConst::RoleID=>$row[SysRoleAuthConst::RoleID],SysRoleAuthConst::Node=>$row[SysRoleAuthConst::Node]];
        }
        $this->CheckEmpty([$cond], ['操作数据为空']);

        $r = $this->SysRoleAuth->update($cond, [SysRoleAuthConst::Status => 0]);
        // hook controller_zx_admin_users_role_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }
    // hook controller_sys_api_stat_end.php
}
?>